import {getBuildTree, getDormTree, getDeviceList, importPerson, importGroupPerson, importPersonProgress, importPersonStatus} from '@/api/buildApi'
import makeTree from '@/util/makeTree'
import {addDorm, getDorm, editDorm, deleteDorm, getDormById} from '@/api/dormApi'
import {getDept, schoolDeptModelList, teacherListByDept} from '@/api/baseDataApi'
const dorm = {
    namespaced: true,
    state: {
        addDormVisible: false,
        addDeviceVisible: false,
        selectTeamVisible: false,
        selectDeviceVisible: false,
        groupVisible: false,
        bedVisible: false,
        addRoomVisible: false,

        editDormVisible: false,
        adminerVisible: false,
        wifiVisible: false,

        wifiSelection: [],
        adminerSelection: [],

        buildTree: [],
        buildRoomList: [],

        PageNum: 1,
        PageSize: 10,
        total: 0,
        dormList: [],

        editDorm: '',
        curDorm: localStorage.curDorm ? JSON.parse(localStorage.curDorm) : '',

        dormTree: [],
        buildFreeTree: [],
        buildFreeRoomList: [],

        deviceList: [],
        selectedDevice: [],

        deptTree: [],
        noDeptTeacherList: [],
        teacherList: []
    },
    getters: {
    },
    mutations: { 
        
        showAddDorm (state) {
            state.addDormVisible = true
        },
        hideAddDorm (state) {
            state.addDormVisible = false
        },
        showEditDorm (state) {
            state.editDormVisible = true
        },
        hideEditDorm (state) {
            state.editDormVisible = false
        },
        showAdminer (state) {
            state.adminerVisible = true
        },
        hideAdminer (state) {
            state.adminerVisible = false
        },
        showWifi (state) {
            state.wifiVisible = true
        },
        hideWifi (state) {
            state.wifiVisible = false
        },
        setCurDorm (state, item) {
            localStorage.curDorm = JSON.stringify(item)
            state.curDorm = item
        },
        removeWifi (state, tag) {
            for (var i = 0; i < state.wifiSelection.length; i++) {
                if (state.wifiSelection[i].id == tag.id) {
                    state.wifiSelection.splice(i, 1)
                }
            }
        },
        removeAdminer (state, tag) {
            for (var i = 0; i < state.adminerSelection.length; i++) {
                if (state.adminerSelection[i].id == tag.id) {
                    state.adminerSelection.splice(i, 1)
                }
            }
        } 
    },
    actions: { 
        getBuildTree ({state}, params) {
            getBuildTree(params).then(res => {
                var data = res.data
                for (var i = data.length - 1; i >= 0; i--) {
                    if (data[i].type == 5) {
                        data.splice(i, 1)
                    }
                }
                if (data) {
                    var list = makeTree({
                        list: data, 
                        parentId: '', 
                        parentIdField: 'parentId', 
                        idField: 'detailId'
                    })
                    state.buildTree = list
                    
                    state.buildRoomList = []
                    for (var item of data) {
                        if (item.type == 6) {
                            state.buildRoomList.push(item)
                        }
                    }
                }
            })
        },
        addDorm ({state, dispatch}, params) {
            return new Promise((resolve, reject) => {
                addDorm(params).then(res => {
                    resolve(res)
                })
            })
        },
        editDorm ({state, commit, dispatch, rootState}, params) {
            return new Promise((resolve, reject) => {
                editDorm(params).then((res) => {
                    resolve(res)
                })
            })
        },
        getDorm ({state, commit, dispatch, rootState, rootGetters}, params) {
            state.PageNum = params.PageNum
            if (state.PageNum < 1) {
                state.PageNum = 1
                return
            }
            state.dormList = []
            getDorm({
                SchoolId: rootGetters.cookiesObj.schoolId,
                PageNum: state.PageNum,
                PageSize: state.PageSize,
                Sorting: 'CreationTime Desc'
            }).then(res => {
                if (res.items.length > 0) {
                    state.dormList = res.items
                    state.total = res.totalCount
                } else {
                    dispatch('getDorm', {PageNum: state.PageNum - 1})
                }
            }).catch(res => {
                console.log(res.status)
            })
        },
        deleteDorm ({state, commit, dispatch}, params) {
            return new Promise((resolve, reject) => {
                deleteDorm(params).then(res => {
                    dispatch('getDorm', {PageNum: state.PageNum})
                    resolve()
                })
            })
        },
        getDormTree ({state}, params) {
            getDormTree(params).then(res => {
                state.buildTree = []
                if (res.status == 200) {
                    var data = res.data
                    var list = makeTree({
                        list: data, 
                        parentId: '', 
                        parentIdField: 'parentId', 
                        idField: 'detailId'
                    })
                    state.buildTree = list
                }
            })
        },
        getRoomFree ({state}, params) {
            getDormTree(params).then(res => {
                if (res.status == 200) {
                    var data = res.data
                  
                    for (var i = data.length - 1; i >= 0; i--) {
                        if (data[i].type == 5) {
                            data.splice(i, 1)
                        }
                    }
                    var list = makeTree({
                        list: data, 
                        parentId: data[0].parentId, 
                        parentIdField: 'parentId', 
                        idField: 'detailId'
                    })
                    state.buildFreeTree = list

                    state.buildFreeRoomList = []
                    for (var item of data) {
                        if (item.type == 6) {
                            state.buildFreeRoomList.push(item)
                        }
                    }
                }
            })
        },
        getDeviceList ({state}, params) {
            getDeviceList(params).then(res => {
                if (res.status == 200) {
                    state.deviceList = res.data
                } else {
                    state.deviceList = []
                }
            })
        },
        importGroupPerson ({state}, params) {
            return new Promise((resolve, reject) => {
                importGroupPerson(params).then(res => {
                    resolve(res)
                }).catch(err => {
                    reject(err)
                })
            })
        },

        getDept ({state}, params) {
            getDept(params).then(res => {
                state.deptTree = res
            })
        },

        schoolDeptModelList ({state, rootState}, params) {
            schoolDeptModelList(params).then(res => {
                var tree = [
                    {
                        schooldept: {name: rootState.common.cookiesObj.schoolName, id: 0},
                        childrenSchoolDept: res.deptList
                    }
                ]
                state.deptTree = tree
                state.noDeptTeacherList = res.noDeptTeacherList

                state.teacherList = res.noDeptTeacherList
            })
        },
        teacherListByDept ({state, rootState}, id) {
            if (id == 0) {
                state.teacherList = state.noDeptTeacherList
                return
            }
            teacherListByDept(id).then(res => {
                if (res) {
                    state.teacherList = res
                } else {
                    state.teacherList = []
                }
            })
        }
    }
}

export default dorm
